package com.longrise.android.byjk.plugins.poster.posterdetail.assist;

import com.longrise.LEAP.Base.Objects.EntityBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by godliness on 2017/9/26.
 * 用于记录某个商品的访问次数
 */

public final class AccessCount {

    private static final String TAG = "AccessCount";

    private final int DEFAULT_COUNT = 16 << 1;

    private ReentrantLock mAccesssLock;
    private ReentrantLock mShareLock;
    private HashMap<String, AtomicInteger> mAccessMap;
    private HashMap<String, AtomicInteger> mShareMap;

    private AccessCount() {
        init();
    }

    private void init() {
        mAccesssLock = new ReentrantLock();
        mShareLock = new ReentrantLock();

        mAccessMap = new HashMap<>(DEFAULT_COUNT);
        mShareMap = new HashMap<>(DEFAULT_COUNT);
    }

    public static AccessCount getInstance() {
        return SingleTon.INSTANCE.getInstance();
    }

    private enum SingleTon {

        INSTANCE;

        private AccessCount mCount;

        SingleTon() {
            mCount = new AccessCount();
        }

        private AccessCount getInstance() {
            return mCount;
        }
    }

    /**
     * 累加一条记录 访问量
     *
     * @param id 商品id
     * @return 返回当前商品的访问数量
     */
    public int accessIn(String id) {
        ReentrantLock lock = this.mAccesssLock;
        int value;
        try {
            lock.lock();
            if (mAccessMap.containsKey(id)) {
                AtomicInteger count = mAccessMap.get(id);
                value = count.incrementAndGet();
                return value;
            }
            AtomicInteger count = new AtomicInteger();
            value = count.incrementAndGet();
            mAccessMap.put(id, count);
            return value;
        } finally {
            mAccesssLock.unlock();
        }
    }

    /**
     * 累加一条记录 分享量
     *
     * @param id 商品id
     * @return 返回当前商品访问量
     */
    public int accessShareIn(String id) {
        ReentrantLock lock = this.mShareLock;
        int value;
        try {
            lock.lock();
            if (mShareMap.containsKey(id)) {
                AtomicInteger count = mShareMap.get(id);
                value = count.incrementAndGet();
                return value;
            }
            AtomicInteger count = new AtomicInteger();
            value = count.incrementAndGet();
            mShareMap.put(id, count);
            return value;
        } finally {
            lock.unlock();
        }
    }

    /**
     * 清空浏览量记录
     */
    public void removeReadAll() {
        ReentrantLock lock = this.mAccesssLock;
        try {
            lock.lock();
            mAccessMap.clear();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 清空分享记录
     */
    public void removeShareAll() {
        ReentrantLock lock = this.mShareLock;
        try {
            lock.lock();
            mShareMap.clear();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 获取记录最大的数量
     */
    public int getMaxCount() {
        ReentrantLock lock = this.mAccesssLock;
        try {
            lock.lock();
            Iterator<AtomicInteger> iterator = mAccessMap.values().iterator();
            int maxCount = 0;
            while (iterator.hasNext()) {
                AtomicInteger count = iterator.next();
                if (count.intValue() > maxCount) {
                    maxCount = count.intValue();
                }
            }
            return maxCount;
        } finally {
            lock.unlock();
        }
    }

    /**
     * 获取记录最多的key
     */
    public String getMaxKey() {
        ReentrantLock lock = this.mAccesssLock;
        try {
            lock.lock();
            Iterator<String> iterator = mAccessMap.keySet().iterator();
            String id = null;
            int count = 0;
            while (iterator.hasNext()) {
                String key = iterator.next();
                int value = mAccessMap.get(key).intValue();
                if (value > count) {
                    id = key;
                    count = value;
                }
            }
            return id;
        } finally {
            lock.unlock();
        }
    }

    /**
     * 获取一共生成记录总数量
     */
    public int getCount() {
        ReentrantLock lock = this.mAccesssLock;
        try {
            lock.lock();
            return mAccessMap.keySet().size();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 获取某条记录的数量
     */
    public int getCount(String key) {
        ReentrantLock lock = this.mAccesssLock;
        try {
            lock.lock();
            return mAccessMap.get(key).intValue();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 形成json格式字符串
     * 没获取一次浏览量信息将removeAll()一次
     */
    public String getJson() {
        ReentrantLock lock = this.mAccesssLock;
        JSONObject json = null;
        Iterator<Map.Entry<String, AtomicInteger>> iterator;
        try {
            lock.lock();
            try {
                json = new JSONObject();
                iterator = mAccessMap.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry<String, AtomicInteger> entry = iterator.next();
                    String key = entry.getKey();
                    String value = entry.getValue().toString();
                    json.put(key, value);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (json != null) {
                return json.toString();
            }
//            removeAll();
            return null;
        } finally {
            lock.unlock();
        }
    }

    /**
     * 获取映射关系
     */
    public Map getMap() {
        ReentrantLock lock = this.mAccesssLock;
        try {
            lock.lock();
            return mAccessMap;
        } finally {
            lock.unlock();
        }
    }

    /**
     * 返回直接符合server数据结构的对象
     */
    public EntityBean getAccessBean() {
        ReentrantLock lock = this.mAccesssLock;
        EntityBean bean;
        Iterator<Map.Entry<String, AtomicInteger>> iterator;
        try {
            lock.lock();
            bean = new EntityBean();
            iterator = mAccessMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, AtomicInteger> entry = iterator.next();
                bean.set(entry.getKey(), String.valueOf(entry.getValue().intValue()));
            }
            return bean;
        } finally {
            lock.unlock();
        }
    }

    /**
     * 获取分享跟踪量 直接返回符合server的数据结构对象
     */
    public EntityBean getShareBean() {
        ReentrantLock lock = this.mShareLock;
        EntityBean bean;
        Iterator<Map.Entry<String, AtomicInteger>> iterator;
        try {
            lock.lock();
            bean = new EntityBean();
            iterator = mShareMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, AtomicInteger> entry = iterator.next();
                bean.set(entry.getKey(), String.valueOf(entry.getValue().intValue()));
            }
            return bean;
        } finally {
            lock.unlock();
        }
    }
}
